package com.gitee.wsl.collections.map

/**
 * A `Map` implementation that maintains the order of the entries.
 * In this implementation order is maintained by original insertion.
 *
 *
 * This implementation improves on the JDK1.4 LinkedHashMap by adding the
 * [MapIterator][org.apache.commons.collections4.MapIterator]
 * functionality, additional convenience methods and allowing
 * bidirectional iteration. It also implements `OrderedMap`.
 * In addition, non-interface methods are provided to access the map by index.
 *
 *
 *
 * The `orderedMapIterator()` method provides direct access to a
 * bidirectional iterator. The iterators from the other views can also be cast
 * to `OrderedIterator` if required.
 *
 *
 *
 * All the available iterators can be reset back to the start by casting to
 * `ResettableIterator` and calling `reset()`.
 *
 *
 *
 * The implementation is also designed to be subclassed, with lots of useful
 * methods exposed.
 *
 *
 *
 * **Note that LinkedMap is not synchronized and is not thread-safe.**
 * If you wish to use this map from multiple threads concurrently, you must use
 * appropriate synchronization. The simplest approach is to wrap this map
 * using [java.util.Collections.synchronizedMap]. This class may throw
 * exceptions when accessed by concurrent threads without synchronization.
 *
 *
 * @param <K> the type of the keys in this map
 * @param <V> the type of the values in this map
 * @since 3.0
</V></K> */
class LinkedMap<K, V> : AbstractLinkedMap<K, V> {
    /**
     * List view of map.
     */
    internal class LinkedMapList<K,V>(
        private val parent: LinkedMap<K,V>
    ) : AbstractList<K>() {
//         override fun clear() {
//            throw UnsupportedOperationException()
//        }

         override fun contains(element: K): Boolean {
            return parent.containsKey(element)
        }

         override fun containsAll(elements: Collection<K>): Boolean {
            return parent.keys.containsAll(elements)
        }

         override fun get(index: Int): K {
            return parent.get(index)?:throw IndexOutOfBoundsException()
        }

         override fun indexOf(element: K): Int {
            return parent.indexOf(element)
        }

         override fun iterator(): Iterator<K> {
            return parent.keys.iterator() as Iterator<K>
        }

         override fun lastIndexOf(element: K): Int {
            return parent.indexOf(element)
        }

         override fun listIterator(): ListIterator<K> {
            return super.listIterator()
        }

         override fun listIterator(index: Int): ListIterator<K> {
            return super.listIterator(index)
        }

//         override fun remove(index: Int): K? {
//            throw java.lang.UnsupportedOperationException()
//        }
//
//         override fun remove(obj: Any?): Boolean {
//            throw java.lang.UnsupportedOperationException()
//        }
//
//         override fun removeAll(coll: Collection<*>?): Boolean {
//            throw java.lang.UnsupportedOperationException()
//        }

        /**
         * @since 4.4
         */
//         override fun removeIf(filter: Predicate<in K?>?): Boolean {
//            throw java.lang.UnsupportedOperationException()
//        }
//
//         override fun retainAll(coll: Collection<*>?): Boolean {
//            throw java.lang.UnsupportedOperationException()
//        }

        override val size: Int
            get() = parent.size


         override fun subList(fromIndex: Int, toIndex: Int): List<K> {
            return super.subList(fromIndex, toIndex)
        }

//         override fun toArray(): Array<Any?> {
//            return parent.keySet().toArray()
//        }
//
//         override fun <T> toArray(array: Array<T?>?): Array<T?> {
//            return parent.keySet().toArray(array)
//        }
    }

    /**
     * Constructs a new empty map with default size and load factor.
     */
    constructor() : super(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR, DEFAULT_THRESHOLD)

    /**
     * Constructs a new, empty map with the specified initial capacity.
     *
     * @param initialCapacity  the initial capacity
     * @throws IllegalArgumentException if the initial capacity is negative
     */
    constructor(initialCapacity: Int) : super(initialCapacity)

    /**
     * Constructs a new, empty map with the specified initial capacity and
     * load factor.
     *
     * @param initialCapacity  the initial capacity
     * @param loadFactor  the load factor
     * @throws IllegalArgumentException if the initial capacity is negative
     * @throws IllegalArgumentException if the load factor is less than zero
     */
    constructor(initialCapacity: Int, loadFactor: Float) : super(initialCapacity, loadFactor)

    /**
     * Constructor copying elements from another map.
     *
     * @param map  the map to copy
     * @throws NullPointerException if the map is null
     */
    constructor(map: Map<K?, V?>) : super(map)

    /**
     * Gets an unmodifiable List view of the keys.
     *
     *
     * The returned list is unmodifiable because changes to the values of
     * the list (using [java.util.ListIterator.set]) will
     * effectively remove the value from the list and reinsert that value at
     * the end of the list, which is an unexpected side effect of changing the
     * value of a list.  This occurs because changing the key, changes when the
     * mapping is added to the map and thus where it appears in the list.
     *
     *
     *
     * An alternative to this method is to use [.keySet].
     *
     *
     * @see .keySet
     * @return The ordered list of keys.
     */
    fun asList(): List<K> {
        return LinkedMapList(this)
    }

    /**
     * Clones the map without cloning the keys or values.
     *
     * @return a shallow clone
     */
//     override fun clone(): LinkedMap<K?, V?>? {
//        return super.clone() as LinkedMap<K?, V?>?
//    }

    /**
     * Gets the key at the specified index.
     *
     * @param index  the index to retrieve
     * @return the key at the specified index
     * @throws IndexOutOfBoundsException if the index is invalid
     */
    fun get(index: Int): K? {
        return getEntry(index)?.key
    }

    /**
     * Gets the value at the specified index.
     *
     * @param index  the index to retrieve
     * @return the value at the specified index
     * @throws IndexOutOfBoundsException if the index is invalid
     */
    fun getValue(index: Int): V? {
        return getEntry(index)?.value
    }

    /**
     * Gets the index of the specified key.
     *
     * @param key  the key to find the index of
     * @return the index, or -1 if not found
     */
    fun indexOf(key: K): Int {
        var key :K = key
        key = convertKey(key) as K
        var i = 0
        var entry = header.after
        while (entry != header) {
            if (isEqualKey(key, entry!!.key)) {
                return i
            }
            entry = entry.after
            i++
        }
        return -1
    }

    /**
     * Deserializes the map in using a custom routine.
     *
     * @param in the input stream
     * @throws IOException if an error occurs while reading from the stream
     * @throws ClassNotFoundException if an object read from the stream cannot be loaded
     */
//    @Throws(IOException::class, java.lang.ClassNotFoundException::class)
//    private fun readObject(`in`: ObjectInputStream) {
//        `in`.defaultReadObject()
//        doReadObject(`in`)
//    }

    /**
     * Removes the element at the specified index.
     *
     * @param index  the index of the object to remove
     * @return the previous value corresponding the `key`,
     * or `null` if none existed
     * @throws IndexOutOfBoundsException if the index is invalid
     */
    fun remove(index: Int): V? {
        return remove(get(index))
    }

    /**
     * Serializes this object to an ObjectOutputStream.
     *
     * @param out the target ObjectOutputStream.
     * @throws IOException thrown when an I/O errors occur writing to the target stream.
     */
//    @Throws(IOException::class)
//    private fun writeObject(out: ObjectOutputStream) {
//        out.defaultWriteObject()
//        doWriteObject(out)
//    }
    
}
